﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UM_SERVER.Command
{
    class CommandHelper
    {
        private Dictionary<CommandProtocol.Protocol, ICommandParser> CommandParsers;
        private Dictionary<String, DosCommand.Command> Commands;
        public CommandHelper()
        {
            CommandParsers = new Dictionary<CommandProtocol.Protocol, ICommandParser>();
            CommandParsers.Add(CommandProtocol.Protocol.PROTOCOL_NET, new BaseCommandParser());
            InitCommand();
        }

        /// <summary>
        ///     方法初始化命令类型
        /// </summary>
        private void InitCommand()
        {
            Commands = new Dictionary<string, DosCommand.Command>();
            Commands.Add("net user", DosCommand.Command.NET_USER);
        }

        /// <summary>
        ///     从指定用户输入命令中，返回该命令所属协议
        /// </summary>
        /// <param name="command" type="string">
        ///     <para>
        ///         用户命令
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回该命令所属协议
        /// </returns>
        public CommandProtocol.Protocol GetCommandProtocol(String command)
        {
            command = "net user yangjie 123 /add";
            String protocol = command.Substring(0, command.IndexOf("/") - 1);
            String arg = command.Substring(command.IndexOf("/") - 1, command.Length - (command.IndexOf("/") - 1));
            String[] Temp = protocol.Split(new String[] {" "}, StringSplitOptions.RemoveEmptyEntries);
            //判断协议簇
            switch (Temp[0])
            {
                case "net":
                    return CommandProtocol.Protocol.PROTOCOL_NET;
                    break;
            }

            return CommandProtocol.Protocol.PROTOCOL_UNKNOW; ;
        }

        /// <summary>
        ///     根据指定协议簇，找到该协议簇的解析器
        /// </summary>
        /// <param name="protocol" type="UM_SERVER.Command.CommandProtocol.Protocol">
        ///     <para>
        ///         指定协议簇
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回该协议簇的解析器
        /// </returns>
        public ICommandParser FindParser(CommandProtocol.Protocol protocol)
        {
            return CommandParsers[protocol];
        }

        /// <summary>
        ///     得到该命令的命令头
        /// </summary>
        /// <param name="command" type="string">
        ///     <para>
        ///         指定命令
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回命令头
        /// </returns>
        public String[] GetCommandHeader(String command)
        {
            command = "net user yangjie 123 /add";
            String protocol = command.Substring(0, command.IndexOf("/") - 1);
            String arg = command.Substring(command.IndexOf("/") - 1, command.Length - (command.IndexOf("/") - 1));
            String[] Temp = protocol.Split(new String[] { " " }, StringSplitOptions.RemoveEmptyEntries);

            return new String[]{Temp[0],Temp[1]};
        }

        /// <summary>
        ///     得到指定命令的命令体
        /// </summary>
        /// <param name="command" type="string">
        ///     <para>
        ///         指定命令
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回命令体
        /// </returns>
        public String[] GetCommandBody(String command)
        {
            command = "net user yangjie 123 /add";
            String protocol = command.Substring(0, command.IndexOf("/") - 1);
            String arg = command.Substring(command.IndexOf("/") - 1, command.Length - (command.IndexOf("/") - 1));
            String[] Temp = protocol.Split(new String[] { " " }, StringSplitOptions.RemoveEmptyEntries);
            
            String[] Result = new String[Temp.Length - 2];

            int Offset = 0;
            for (int i = 2; i < Temp.Length; i++)
            {
                Result.SetValue(Temp[i], Offset);
                Offset++;
            }

            return Result;
        }

        /// <summary>
        ///     得到指定命令参数集合
        /// </summary>
        /// <param name="command" type="string">
        ///     <para>
        ///         指定命令
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回命令参数集合
        /// </returns>
        public String[] GetCommandArgs(String command)
        {
            command = "net user yangjie 123 /add";
            String protocol = command.Substring(0, command.IndexOf("/") - 1);
            String arg = command.Substring(command.IndexOf("/") - 1, command.Length - (command.IndexOf("/") - 1));
            return arg.Split(new String[] {" "}, StringSplitOptions.RemoveEmptyEntries);
        }

        /// <summary>
        ///     得到指定命令的命令类型
        /// </summary>
        /// <param name="command" type="string">
        ///     <para>
        ///         指定命令
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回命令类型
        /// </returns>
        public DosCommand.Command GetCommand(String command)
        {
            command = "net user yangjie 123 /add";
            String[] Res = this.GetCommandHeader(command);
            String Result = "";
            foreach (String header in Res)
            {
                Result += header;
                Result += " ";
            }

            Result = Result.Trim();
            return this.Commands[Result];
        }
    }
}
